<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据库输入界面</title>
    <style>
        h1 {
            color: #333;
        }

        #fileInput {
            margin: 20px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }

        #uploadBtn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }

        #uploadBtn:hover {
            background-color: #45a049;
        }

        #status {
            margin: 10px 0;
            font-size: 16px;
            color: #555;
        }

        #uploadedImage img {
            margin-top: 20px;
            max-width: 100%;
            height: auto;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .progress-bar {
            width: 100%;
            max-width: 300px;
            background-color: #ddd;
            border-radius: 5px;
            overflow: hidden;
            margin-top: 20px;
        }

        .progress {
            width: 0;
            height: 10px;
            background-color: #4CAF50;
            transition: width 0.4s ease;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f3f4f6;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            background-color: #fff;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            text-align: center;
        }
        h2 {
            color: #333;
        }
        .input-group {
            margin: 1rem 0;
        }
        .input-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: bold;
            color: #555;
        }
        .input-group input {
            width: 100%;
            padding: 0.5rem;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .submit-btn {
            background-color: #4CAF50;
            color: white;
            padding: 0.7rem 1.5rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 1rem;
        }
        .submit-btn:hover {
            background-color: #45a049;
        }
        .message {
            margin-top: 1rem;
            font-weight: bold;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>数据输入界面</h2>
    <div class="input-group">
        <label for="database">组名</label>
        <input type="number" id="database" placeholder="例如: 0">
    </div>
    <div class="input-group">
        <label for="key">手机名</label>
        <input type="text" id="key" placeholder="请输入手机名">
    </div>
    <div class="input-group">
        <label for="value">待发内容</label>
        <input type="text" id="value" placeholder="请输入待发送的内容">
    </div>
    <div class="input-group">
        <label for="value">要转发的群聊</label>
        <input type="text" id="qun" placeholder="请输入待发送的内容">
    </div>
    <button class="submit-btn" onclick="setKey()">提交</button>
    <div class="message" id="message"></div>
    <h1>图片上传</h1>
    <input type="file" id="fileInput" accept="image/png, image/jpeg, image/jpg, image/gif"/>
    <button id="uploadBtn">上传</button>
    <p id="status"></p>

    <!-- 进度条 -->
    <div class="progress-bar">
        <div class="progress" id="progress"></div>
    </div>

    <!-- 显示上传结果 -->
    <div id="uploadedImage"></div>
</div>

<script>
    async function setKey() {
        const database = document.getElementById('database').value;
        const key = document.getElementById('key').value;
        const value = document.getElementById('value').value;
        const qun = document.getElementById('qun').value;

        if (!database || !key || !value) {
            document.getElementById('message').innerText = '请填写所有字段';
            return;
        }

        const response = await fetch('/set_key', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ database, key, value, qun })
        });

        const result = await response.json();
        if (result.status === 'success') {
        const messageElement = document.getElementById('message');
        messageElement.innerText = '数据已成功存储到 Redis';

        // 设置 3 秒后清空消息
        setTimeout(() => {
        messageElement.innerText = '';
        }, 1500);
        } else {
        document.getElementById('message').innerText = '发生错误: ' + result.message;
        }
    }
    // function uploadImage() {
    //         const fileInput = document.getElementById('imageInput');
    //         const file = fileInput.files[0];
    //         if (!file) {
    //             alert('Please select an image!');
    //             return;
    //         }
    //
    //         const formData = new FormData();
    //         formData.append('image', file);
    //
    //         fetch('http://127.0.0.1:5000/upload', {  // 替换 <your-server-ip> 为服务器 IP
    //             method: 'POST',
    //             body: formData
    //         })
    //         .then(response => response.json())
    //         .then(data => {
    //             if (data.image_id) {
    //                 document.getElementById('result').textContent =
    //                     `Upload successful! Image ID: ${data.image_id}`;
    //             } else {
    //                 alert('Error: ' + data.error);
    //             }
    //         })
    //         .catch(error => console.error('Error:', error));
    //     }
        const uploadBtn = document.getElementById('uploadBtn');
        const fileInput = document.getElementById('fileInput');
        const statusText = document.getElementById('status');
        const uploadedImage = document.getElementById('uploadedImage');
        const progressBar = document.getElementById('progress');

        uploadBtn.addEventListener('click', async () => {
            const file = fileInput.files[0]; // 获取选中的文件

            if (!file) {
                statusText.textContent = '请选择要上传的图片';
                return;
            }

            // 验证文件大小（10MB 限制）
            if (file.size > 10 * 1024 * 1024) {
                statusText.textContent = '文件大小超过10MB限制';
                return;
            }

            const formData = new FormData();
            formData.append('image', file); // 将文件添加到 FormData

            try {
                statusText.textContent = '上传中...';
                progressBar.style.width = '0%';

                // 发起 POST 请求，带有上传进度监听
                const response = await fetch('http://127.0.0.1:5000/upload', {
                    method: 'POST',
                    body: formData,
                });

                const result = await response.json();

                if (response.ok) {
                    statusText.textContent = '上传成功!';
                    progressBar.style.width = '100%';

                    // 显示上传的图片
                    uploadedImage.innerHTML = `
                        <p>上传的图片地址：${result.file_path}</p>
                    `;
                } else {
                    statusText.textContent = `错误: ${result.error}`;
                    progressBar.style.width = '0%';
                }
            } catch (error) {
                statusText.textContent = `错误: ${error.message}`;
                progressBar.style.width = '0%';
            }
        });
</script>
</body>
</html>
